import { getAllLiveStreaming, orderLiveStreaming } from "@/api/liveStreaming";
import { getCarousel, getFireUser } from "@/api/home/home";
// import Slideshow from "@/components/slideshow/SlideView.vue";
import { useUserStoreHook } from "@/store/modules/user";
import { ref, onMounted } from "vue";
import { ElMessage } from "element-plus";
import { getAllArticle } from "@/api/community/index";

export function useHome() {
  const userStore = useUserStoreHook();

  // ------------------ 直播列表 ---------------
  const liveStreamingRoomList = ref<any>([]);

  async function getLiveStreamingRoomListFun() {
    const res = await getAllLiveStreaming();
    if (res.code == 201) return;

    liveStreamingRoomList.value = res.data;

    const _len = liveStreamingRoomList.value?.length;

    if (_len < 7) {
      for (let i = 0; i < 7 - _len; i++) {
        liveStreamingRoomList.value.push(liveStreamingRoomList.value[0]);
      }
    }
  }

  async function orderLiveStreamingFun(item: any) {
    const res = await orderLiveStreaming({
      userId: userStore.getUserInfo?.userId,
      liveStreamingRoomId: item.id
    });

    if (res.code == 200) {
      ElMessage.success("预约成功");
      getLiveStreamingRoomListFun();
      return;
    }

    ElMessage.error(res.msg);
  }

  // -------------------

  // -------------- 轮播图 ---------------
  const carouselList = ref([]);
  async function getCarouselFun() {
    const res = await getCarousel();

    carouselList.value = res.data;
  }

  // ------------------- 热门教师 -----------------------------
  const hotTeacherList = ref<any>([]);
  async function getHotTeacherFun() {
    const res = await getFireUser({ page: 1 });

    if (res.code == 200) {
      hotTeacherList.value = res.data.records.slice(0, 5);
    }
  }

  // ----- 社区 ------
  const communityList = ref<any>([]);
  async function getCommunityFun() {
    const res = await getAllArticle({ pageSize: 999 });

    if (res.code == 200) {
      communityList.value = res.data.records.slice(0, 4);
    }
  }

  onMounted(() => {
    getLiveStreamingRoomListFun();
    getCarouselFun();
    getHotTeacherFun();
    getCommunityFun();
  });
  return {
    userStore,
    liveStreamingRoomList,
    carouselList,
    hotTeacherList,
    communityList,
    orderLiveStreamingFun
  };
}
